package com.db.mongo;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;

public class MongoDBFactory {

	@SuppressWarnings("resource")
	public static MongoDatabase create(String ip, int port, String db) {
		// 连接到 mongodb 服务
		MongoClient mongoClient = new MongoClient(ip, port);
		// 连接到数据库,如果指定的数据库不存在，mongo会自动创建数据库
		MongoDatabase mongoDatabase = mongoClient.getDatabase(db);
		System.out.println(String.format("Connect to database [%s] successfully", mongoDatabase.getName()));
		return mongoDatabase;
	}

}
